package prictise.com.application1.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import prictise.com.application1.R;
import prictise.com.application1.utils.LogcatUtils;
import prictise.com.application1.utils.ToastUtil;

import static android.app.Activity.RESULT_OK;

/**
 * @Author zhi
 * @Date 2019/8/7 5:10
 * @Describer
 */
public class Fragment1 extends Fragment {

  private final String TAG = Fragment1.class.getSimpleName();
  private PictureUpdateBr mUpdatePictureBR;

  @Override
  public void onAttach(Context context) {
    super.onAttach(context);
    ToastUtil.showShort("onAttach");
    LogcatUtils.showDLog(TAG, "Fragment1 - onAttach(context)");
  }

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    LogcatUtils.showDLog(TAG, "Fragment1 - onAttach(activity)");
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
      onAttachToContext(activity);
    }
  }

  /*
   * Called when the fragment attaches to the context
   */
  protected void onAttachToContext(Context context) {
    //do something
  }


  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LogcatUtils.showDLog(TAG, "Fragment1 - onCreate()");
  }

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    LogcatUtils.showDLog(TAG, "Fragment1 - onCreateView()");
    View root = inflater.inflate(R.layout.fragment_test1, container, false);
    root.findViewById(R.id.bt_jump_test).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
//        Intent intent = new Intent(getActivity(), TestActivity.class);
//        intent.setAction("com.example.testresult.TestActivity");
//        intent.addCategory("com.test.second.category");
//        getActivity().startActivityForResult(intent,100);
        LogcatUtils.showDLog(TAG, "Fragment1 - bt_jump_test");
        EventBus.getDefault().postSticky(new MessageUpdatePic());
      }
    });
    mUpdatePictureBR = new PictureUpdateBr();
    // 2. 设置接收广播的类型
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("APP_UPDATE_PIC");
    // 3. 动态注册：调用Context的registerReceiver（）方法
    getActivity().getApplicationContext().registerReceiver(mUpdatePictureBR, intentFilter);
    return root;
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100 && resultCode == RESULT_OK) {
      LogcatUtils.showDLog(TAG, "Fragment1 - onActivityResult()");
    }
  }

  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    LogcatUtils.showDLog(TAG, "Fragment1 - onActivityCreated()");


  }
  class PictureUpdateBr extends BroadcastReceiver {

    @SuppressLint("AvoidUsageApiCheck")
    @Override
    public void onReceive(Context context, Intent intent) {
      Log.d(TAG,"update pic.");
    }
  }
  @Override
  public void onStart() {
    super.onStart();
    LogcatUtils.showDLog(TAG, "Fragment1 - onStart()");
  }

  @Subscribe
  public void getTest(String a){

  }

  @Override
  public void onResume() {
    super.onResume();
    LogcatUtils.showDLog(TAG, "Fragment1 - onResume()");
    EventBus.getDefault().register(this);
  }

  @Override
  public void onPause() {
    super.onPause();
    LogcatUtils.showDLog(TAG, "Fragment1 - onPause()");
  }

  @Override
  public void onStop() {
    super.onStop();
    LogcatUtils.showDLog(TAG, "Fragment1 - onStop()");
  }

  @Override
  public void onDestroyView() {
    super.onDestroyView();
    LogcatUtils.showDLog(TAG, "Fragment1 - onDestroyView()");
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    LogcatUtils.showDLog(TAG, "Fragment1 - onDestroy()");
    getActivity().getApplicationContext().unregisterReceiver(mUpdatePictureBR);
    EventBus.getDefault().unregister(this);
  }

  @Override
  public void onDetach() {
    super.onDetach();
    LogcatUtils.showDLog(TAG, "Fragment1 - onDetach()");
  }
}
